package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 活动列表项数据传输对象
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "活动列表项信息")
public class ActivityListDTO {

    @NotNull(message = "活动ID不能为空")
    @Schema(description = "活动唯一标识", example = "1")
    private Long id;

    @NotBlank(message = "活动标题不能为空")
    @Size(max = 200, message = "活动标题长度不能超过200个字符")
    @Schema(description = "活动标题", example = "2024年春季科技创新营")
    private String title;

    @NotNull(message = "开始日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动开始日期", example = "2024-03-01")
    private LocalDate startDate;

    @NotNull(message = "结束日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动结束日期", example = "2024-03-07")
    private LocalDate endDate;

    @Min(value = 1, message = "活动天数至少为1天")
    @Schema(description = "活动总天数", example = "7")
    private Integer totalDays;

    @NotNull(message = "基地ID不能为空")
    @Schema(description = "关联基地ID", example = "1")
    private Long baseId;

    @NotBlank(message = "基地名称不能为空")
    @Schema(description = "基地名称", example = "北京科技教育基地")
    private String baseName;

    @NotBlank(message = "活动地址不能为空")
    @Schema(description = "活动举办地址", example = "北京市海淀区中关村大街1号")
    private String address;

    @NotNull(message = "开始时间不能为空")
    @JsonFormat(pattern = "HH:mm:ss")
    @Schema(description = "每日开始时间", example = "09:00:00")
    private LocalTime startTime;

    @NotNull(message = "报名截止时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "报名截止时间", example = "2024-02-20 18:00:00")
    private LocalDateTime registrationDeadline;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "活动发布时间", example = "2024-01-15 10:00:00")
    private LocalDateTime publishTime;

    @NotBlank(message = "目标受众不能为空")
    @Schema(description = "目标受众群体", example = "中小学生")
    private String targetAudience;

    @NotNull(message = "是否免费必须指定")
    @Schema(description = "是否免费活动", example = "true")
    private Boolean isFree;

    @Size(max = 500, message = "活动描述长度不能超过500个字符")
    @Schema(description = "活动简要描述", example = "面向中小学生的科技创新实践营")
    private String description;

    @Min(value = 1, message = "最大参与人数至少为1")
    @Schema(description = "最大参与人数", example = "50")
    private Integer maxParticipants;

    @Min(value = 0, message = "当前参与人数不能为负数")
    @Schema(description = "当前参与人数", example = "25")
    private Integer currentParticipants;

    @NotNull(message = "创建人ID不能为空")
    @Schema(description = "创建人用户ID", example = "1001")
    private Long creatorId;

    @NotBlank(message = "创建人姓名不能为空")
    @Schema(description = "创建人姓名", example = "张老师")
    private String creatorName;

    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    @Schema(description = "创建人联系电话", example = "13800138000")
    private String creatorPhone;

    @Email(message = "邮箱格式不正确")
    @Schema(description = "创建人邮箱", example = "teacher@example.com")
    private String creatorEmail;

    @NotNull(message = "报名状态必须指定")
    @Schema(description = "当前用户是否已报名", example = "false")
    private Boolean hasJoined;

    /**
     * 获取剩余名额
     *
     * @return 剩余可报名人数
     */
    @Schema(description = "剩余名额", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getRemainingSlots() {
        if (maxParticipants == null || currentParticipants == null) {
            return 0;
        }
        return Math.max(0, maxParticipants - currentParticipants);
    }
}
